package ru.passivemoney.strategy.processors.williams;

import java.util.Set;

import com.dukascopy.api.IBar;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

import ru.passivemoney.common.UtilsOrders;
import ru.passivemoney.indicator.AIndicator;
import ru.passivemoney.root.ContextHolder;
import ru.passivemoney.strategy.Prognosis;
import ru.passivemoney.strategy.processors.IProcessor;

public class FirstBulletProcessor implements IProcessor {

	enum BarCode{code_11, code_12, code_13, code_21, code_22, code_23, code_31, code_32, code_33};
	enum BarTrend{plus, minus, zero};
	enum BarProfitunities{pp,pm,mp,mm};
	
	private OfferSide offerSide = OfferSide.ASK;
	
	@Override
	public Prognosis processBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
		IBar bar;
		if(offerSide.equals(OfferSide.ASK)){
			bar = askBar;
		}else{
			bar = bidBar;
		}
		double diff = bar.getHigh() - bar.getLow();
		double third = diff / 3;
		double thirdBorder = bar.getLow() + third;
		double secondBorder = bar.getLow() + third + third;
		
		int openCode = getCode(bar.getOpen(), thirdBorder, secondBorder);
		int closeCode = getCode(bar.getOpen(), thirdBorder, secondBorder);
		
		BarCode barCode = BarCode.valueOf("code_" + openCode + closeCode);
		
		IBar previousBar = UtilsOrders.getPreviousBar(instrument, period, offerSide);
		double middle = bar.getLow() + diff / 2;
		BarTrend barTrend = getTrend(previousBar, middle);
		
		BarProfitunities barProfitunities = null;
		String firstLetter = getProfitunitySign(previousBar.getVolume(), bar.getVolume());
		String secondLetter = null;
		try {
			double mfiCurrentBar = ContextHolder.getContext().getIndicators().mfi(instrument, period, offerSide, 14, 1);
			double mfiPreviousBar = ContextHolder.getContext().getIndicators().mfi(instrument, period, offerSide, 14, 2);
			secondLetter = getProfitunitySign(mfiPreviousBar, mfiCurrentBar);
		} catch (JFException e) {
			e.printStackTrace();
		}
		if(firstLetter != null && secondLetter != null){
			barProfitunities = BarProfitunities.valueOf(firstLetter + secondLetter);
		}
		
		
		
		return null;
	}
	
	private int getCode(double val, double thirdBorder, double secondBorder){
		int out;
		if(val < thirdBorder) out = 3;
		else if(val < secondBorder) out = 2;
		else out = 1;
		
		return out;
	}
	
	private BarTrend getTrend(IBar previuosBar, double middle){
		if(middle > previuosBar.getHigh()) return BarTrend.plus;
		else if(middle < previuosBar.getLow()) return BarTrend.minus;
		else return BarTrend.zero;
	}
	
	private String getProfitunitySign(double previous, double current){{
		if(previous < current) return "p";
		else if(previous > current) return "m";
		else return null;
	}
		
	}

	@Override
	public void setIndicators(Set<AIndicator> indicators) {
	}

	@Override
	public void init() {
	}

}
